package com.wsc.wisdomchina.ai.widget;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsc.wisdomchina.R;


/**
 * Created by liuhaigang on 2016/12/2.
 */
public class MyDialog {
    private static TextView text;
    private static Dialog dialog;
    private static   MyDialog myDialog;
    public static MyDialog getMyDialogIntance(Context context){
        if(myDialog==null){
            myDialog=new MyDialog();
        }
//        dialog= getIntance(context);
        return myDialog;
    }
    private static   Dialog getIntance(Context context) {
        if (dialog == null) {
            dialog = new Dialog(context, R.style.dialog_noframe);
        }
        View view = LayoutInflater.from(context).inflate(R.layout.layout_loading, null);
        view.findViewById(R.id.dialog_layout).getBackground().setAlpha(200);
        dialog.setContentView(view);

        // 不可以取消
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }
    public void dialogShow(){
        if(!dialog.isShowing()){
            dialog.show();
        }
    }
    public void setProgress(int progress) {
        text.setText(progress * 100 + "%");
    }

    public void dialogDismiss() {
        if(dialog.isShowing()){
            dialog.dismiss();
        }
    }
    @SuppressLint("InflateParams")
    public static Dialog getLoadDialog(Context context, String str) {

        Dialog myDialog = new Dialog(context, R.style.dialog_noframe);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_loading, null);
//        view.findViewById(R.id.dialog_layout).getBackground().setAlpha(200);
        TextView text = (TextView) view.findViewById(R.id.dialog_text);
        ImageView imageView = (ImageView) view.findViewById(R.id.dialog_img);
        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
        animationDrawable.start();

        myDialog.setContentView(view);
        if (TextUtils.isEmpty(str)) {
            text.setVisibility(View.GONE);
        } else {
            text.setText(str);
        }
        // 不可以取消
        myDialog.setCancelable(true);
        myDialog.setCanceledOnTouchOutside(false);
        return myDialog;
    }

}
